import { createRouter, createWebHistory } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { store } from "@/store";

// 定义基础路由
const baseRoutes = [
  {
    path: "/",
    redirect: "/index"
  },
  {
    path: "/index",
    name: "Index",
    component: () =>
      import(/* webpackChunkName: "index" */ "@/views/index.vue"),
    meta: {
      requiresAuth: true, // 需要登录
      title: "首页",
      keepAlive: true, // 需要缓存
      hidden: false, // 路由是否隐藏
      icon: "", // 路由图标
      hasPerm: [], // 权限模块
      type: "menu", // menu:菜单 button:按钮
      affix: false // 固定在标签栏
    }
  },
  {
    path: "/commandPage",
    name: "CommandPage",
    component: () =>
      import(
        /* webpackChunkName: "command-index" */ "@/views/commandPage/router.vue"
      ),
    meta: {
      requiresAuth: true,
      title: "路由导航"
    },
    children: [
      {
        path: "index", // 相对路径
        name: "commandPagePage",
        component: () =>
          import(
            /* webpackChunkName: "command-add" */ "@/views/commandPage/index.vue"
          ),
        meta: {
          requiresAuth: true,
          title: "监控命令",
          keepAlive: false, // 不需要缓存
          hidden: false, // 路由是否隐藏
          icon: "", // 路由图标
          hasPerm: [], // 权限模块
          type: "menu", // menu:菜单 button:按钮
          affix: false // 固定在标签栏
        }
      },
      {
        path: "add", // 相对路径
        name: "commandPageAdd",
        component: () =>
          import(
            /* webpackChunkName: "command-add" */ "@/views/commandPage/add.vue"
          ),
        meta: {
          requiresAuth: true,
          title: "新增监控命令",
          keepAlive: false, // 不需要缓存
          hidden: false, // 路由是否隐藏
          icon: "", // 路由图标
          hasPerm: [], // 权限模块
          type: "menu", // menu:菜单 button:按钮
          affix: false // 固定在标签栏
        }
      },
      {
        path: "edit", // 相对路径
        name: "commandPageEdit",
        component: () =>
          import(
            /* webpackChunkName: "command-edit" */ "@/views/commandPage/edit.vue"
          ),
        meta: {
          requiresAuth: true,
          title: "修改监控命令",
          keepAlive: false, // 不需要缓存
          hidden: false, // 路由是否隐藏
          icon: "", // 路由图标
          hasPerm: [], // 权限模块
          type: "menu", // menu:菜单 button:按钮
          affix: false // 固定在标签栏
        }
      },
      {
        path: "warn",
        name: "commandPageWarn",
        component: () =>
          import(
            /* webpackChunkName: "WarningPage" */ "@/views/commandPage/warn.vue"
          ),
        meta: {
          requiresAuth: true,
          title: "监控警告",
          keepAlive: false, // 不需要缓存
          hidden: false, // 路由是否隐藏
          icon: "", // 路由图标
          hasPerm: [], // 权限模块
          type: "menu", // menu:菜单 button:按钮
          affix: false // 固定在标签栏
        }
      }
    ]
  },

  {
    path: "/login",
    name: "Login",
    component: () => import(/* webpackChunkName: "login" */ "@/page/login.vue"),
    meta: {
      hidden: false,
      title: "登录",
      keepAlive: false, // 不需要缓存
      hidden: false, // 路由是否隐藏
      icon: "", // 路由图标
      hasPerm: [], // 权限模块
      type: "menu", // menu:菜单 button:按钮
      affix: false // 固定在标签栏
    }
  },
  {
    path: "/404",
    name: "NotFound",
    component: () => import(/* webpackChunkName: "404" */ "@/page/404.vue"),
    meta: { hidden: true, title: "页面未找到" }
  },
  {
    path: "/:pathMatch(.*)*",
    redirect: "/404"
  }
];

// 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: baseRoutes
});

// 白名单路由
const whiteList = ["/login", "/404"];

// 路由守卫
router.beforeEach(async (to, from, next) => {
  try {
    const token = store.useUserStore().token;

    // NProgress.start();

    // 白名单直接放行
    if (whiteList.includes(to.path)) {
      if (token && to.path === "/login") {
        return next("/index"); // 已登录用户重定向到首页
      }
      return next();
    }

    // 检查是否需要登录
    if (to.meta.requiresAuth && !token) {
      NProgress.done();
      return next("/login");
    }

    next();
  } catch (error) {
    console.error("路由守卫出错:", error);
    NProgress.done();
    next("/404");
  }
});

// 全局后置钩子
router.afterEach(() => {
  NProgress.done();
});

export default router;
